package io.elasticjob.lite.console;

import io.elasticjob.lite.console.restful.config.JerseyConfig;
import org.glassfish.jersey.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.DispatcherServlet;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: ldz
 * @Date: 2018-05-04 14:01
 */
@SpringBootApplication
//@EnableEurekaClient
public class ElasticJobConsoleApplication {

//    @Value("${server.port}")
//    private static int port;

    public static void main(String[] args) {
        SpringApplication.run(ElasticJobConsoleApplication.class, args);
    }

    @Bean
    public ServletRegistrationBean jerseyServlet() {
//        ServletContainer container = new ServletContainer(Resourc);
        ServletRegistrationBean registration = new ServletRegistrationBean(new ServletContainer(), "/api/*");
        registration.addInitParameter(ServletProperties.JAXRS_APPLICATION_CLASS, JerseyConfig.class.getName());
        registration.addInitParameter("com.sun.jersey.config.property.packages", "io.elasticjob.lite.lifecycle.restful,io.elasticjob.lite.console.restful");
//        registration.addInitParameter("com.sun.jersey.config.property.resourceConfigClass", PackagesResourceConfig.class.getName());
        registration.addInitParameter("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString());
        registration.addInitParameter("resteasy.scan.providers", Boolean.TRUE.toString());
        registration.addInitParameter("resteasy.use.builtin.providers", Boolean.FALSE.toString());
        registration.setLoadOnStartup(2);
        return registration;
    }

//    private ServletHolder getServletHolder(final String packages) {
//        ServletHolder result = new ServletHolder(com.sun.jersey.spi.container.servlet.ServletContainer.class);
//        System.out.println(Joiner.on(",").join(RestfulServer.class.getPackage().getName(), packages));
//        result.setInitParameter(PackagesResourceConfig.PROPERTY_PACKAGES, Joiner.on(",").join(RestfulServer.class.getPackage().getName(), packages));
//        result.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", PackagesResourceConfig.class.getName());
//        result.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", Boolean.TRUE.toString());
//        result.setInitParameter("resteasy.scan.providers", Boolean.TRUE.toString());
//        result.setInitParameter("resteasy.use.builtin.providers", Boolean.FALSE.toString());
//        return result;
//    }

    @Bean
    public ServletRegistrationBean dispatcherRegistration(DispatcherServlet dispatcherServlet) {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet);
        List<String> urls = new ArrayList<>();

        urls.add("/*");
        registration.setUrlMappings(urls);
        return registration;
    }

}
